package com.android.baseconfig.base;

import android.app.Activity;
import android.text.TextUtils;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.TUtil;
import com.android.baseconfig.constact.AppGlobalHost;
import com.android.baseconfig.data.http.okhttp.OkHttpManager;
import com.android.baseconfig.data.http.okhttp.util.OkHttpTag;
import com.android.baseconfig.data.http.retrofit2.RetrofitManager;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;

public class BaseRetrofit<T> {

    protected Activity activity;
    protected T api;
    private OkHttpTag okHttpTag;

    private Retrofit retrofit;

    private Class<?> clazz;

    public BaseRetrofit() {
        clazz = TUtil.getTT(this);
        build();
        api();
        RxBus3.getInstance().register("switch_net", (Consumer<Boolean>) aBoolean -> {
            Logc.d("switch net recv");
            build();
        }, this.getClass());
    }

    public T api(){
        return api;
    }

    private void build() {
        String host = AppGlobalHost.getHost();
        if (host.startsWith("https://")) {
            api = buildHttps();
        } else if (host.startsWith("http://")) {
            api = buildHttp();
        }
    }

    public void setContext(Activity a) {
        activity = a;
        okHttpTag = new OkHttpTag(a.getClass().getName());
        okHttpTag.setActivity(a);
    }

    public T getRetrofit(Class clazz) {
        retrofit = RetrofitManager.getRetrofit(okHttpTag);
        if (retrofit == null) {
            throw new NullPointerException("createRetrofit method can't return null !");
        }
        return (T) retrofit.create(clazz);
    }

    public T buildHttps() {
        Retrofit.Builder builder = RetrofitManager.getRetrofitBuilder(OkHttpManager.getClient(), AppGlobalHost.getHttps());
        Retrofit retrofit = builder.build();
        RetrofitManager.createRetrofit(retrofit, okHttpTag);
        return retrofit.create((Class<T>) clazz);
    }

    public T buildHttp() {
        Retrofit.Builder builder = RetrofitManager.getRetrofitBuilder(OkHttpManager.getClient(), AppGlobalHost.getHttp());
        Retrofit retrofit = builder.build();
        RetrofitManager.createRetrofit(retrofit, okHttpTag);
        return retrofit.create((Class<T>) clazz);
    }

    public void noProgressBar(String path) {
        noProgressBar(path, null);
    }

    public void noProgressBar(String path, String message) {
        OkHttpTag okTag = okHttpTag.clone();
        okTag.setPath(path);
        if (!TextUtils.isEmpty(message)) {
            okTag.setShowMsg(message);
        }
        okTag.setNeedProgressBar(false);
        setOkTag(okTag);
    }

    public void needProgressBar(String path) {
        OkHttpTag okTag = okHttpTag.clone();
        okTag.setPath(path);
        okTag.setNeedProgressBar(true);
        setOkTag(okTag);
    }

    public void setTag(Object obj, String path) {
        OkHttpTag okTag = okHttpTag.clone();
        okTag.setPath(path);
        okTag.setTag(obj);
        okTag.setNeedProgressBar(true);
        setOkTag(okTag);
    }

    public void setActivity(Activity activity, String path) {
        OkHttpTag okTag = okHttpTag.clone();
        okTag.setPath(path);
        okHttpTag.setActivity(activity);
        okTag.setNeedProgressBar(true);
        setOkTag(okTag);
    }

    public void setProgressMessage(Activity activity, String msg, String path) {
        if (activity != null) {
            this.setActivity(activity, path);
        }
        OkHttpTag okTag = okHttpTag.clone();
        okTag.setPath(path);
        okTag.setShowMsg(msg);
        setOkTag(okTag);
    }

    private void setOkTag(OkHttpTag okTag) {
        if (retrofit != null) {
            RetrofitManager.setOkHttp(retrofit, okTag);
        }
    }

    protected <T> ObservableTransformer<T, T> applySchedulers() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    protected <T> ObservableTransformer<T, T> applySchedulers1() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .observeOn(Schedulers.io());
            }
        };
    }

    public void onDestroy() {
        RxBus3.getInstance().unregister(this.getClass());
    }
}
